package com.rhok.wthru_client;

import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;
import android.util.Log;

public class GlobalConfiguration {
	
	public static final String SENDER_ID = "487505963150";
	public static final String CONF_KEY_NAME = "name";
	public static final String CONF_KEY_PHONE = "phone";
	public static final String CONF_KEY_UNIQUE_ID = "uid";
	public static final String CONF_KEY_SCHEMA_VERSION = "schema_version";

	private static final String LOG_TOPIC = "WTHRU_GlobalConfiguration";
	
	private static final String DEFAULT_PREFS_NAME = "GlobalConfiguration";
	private static final Integer CURRENT_SCHEMA_VERSION = 1;
	
	private Location lastKnownLocation = null;
	
	
	private SharedPreferences m_SharedPreferences = null;
	
	private static GlobalConfiguration m_Instance = null;
	
	private GlobalConfiguration(Context context, String prefsName) {
		m_SharedPreferences = context.getSharedPreferences(prefsName, 0);
	}
	
	private void generateInitialConfigurationIfRequired() {
		int currentSchemaVersion = 0;
		currentSchemaVersion = m_SharedPreferences.getInt(CONF_KEY_SCHEMA_VERSION, 0);
		if (currentSchemaVersion == CURRENT_SCHEMA_VERSION) {
			Log.i(LOG_TOPIC, "Configuration found. Schema version: " + currentSchemaVersion);
			
			// No need to recreate - already at the latest version
			return;
		} 
		
		// XXX: Handle schema upgrades
		
		Log.i(LOG_TOPIC, "No configuration found, need to create configuration");
		SharedPreferences.Editor editor = m_SharedPreferences.edit();
		int schemaVersion = CURRENT_SCHEMA_VERSION;
		editor.putInt(CONF_KEY_SCHEMA_VERSION, schemaVersion);
		editor.putString("servers", "http://localhost:8082");
		
		editor.commit();
		Log.i(LOG_TOPIC, "Configuration created. Schema vesrion: " + schemaVersion);
	}
	
	public String getString(String key) {
		return m_SharedPreferences.getString(key, "");
	}
	
	public String getString(String key, String defValue) {
		return m_SharedPreferences.getString(key, defValue);
	}
	
	public void init() {
		generateInitialConfigurationIfRequired();
	}
	
	public boolean isRegistered () {
		return m_SharedPreferences.contains(CONF_KEY_UNIQUE_ID);
	}
	
	public void setLocalInfo(BasicContactInfo selfInfo, boolean overwrite) {
		if (isRegistered() && !overwrite)
		{
			Log.i(LOG_TOPIC, "Registration info already exist, ignore request");
			return;
		}
		
		SharedPreferences.Editor editor = m_SharedPreferences.edit();
		editor.putString(CONF_KEY_PHONE, selfInfo.getPhone());
		editor.putString(CONF_KEY_NAME, selfInfo.getContactName());
		editor.commit();
	}
	
	public static synchronized GlobalConfiguration getInstance(Context context) {
		if (null == m_Instance) {
			m_Instance = new GlobalConfiguration(context, DEFAULT_PREFS_NAME);
			m_Instance.init();
		}
		return m_Instance;
	}

	public Location getLastKnownLocation() {
		return lastKnownLocation;
	}

	public void setLastKnownLocation(Location lastKnownLocation) {
		this.lastKnownLocation = lastKnownLocation;
		Log.d("Maps", "Set my location: " + lastKnownLocation.toString());
	}

	public void setUuid(String uuid) {
		SharedPreferences.Editor editor = m_SharedPreferences.edit();
		editor.putString(CONF_KEY_UNIQUE_ID, uuid);
		editor.commit();
	}

	
	
	
	

}
